import pymongo

# conn = pymongo.MongoClient('192.168.9.208', 27017, username="zdiai",
#                            password="zdiai@123", authSource="Repo_history", authMechanism="SCRAM-SHA-1")

conn = pymongo.MongoClient('zhenduisdc.mongodb.rds.aliyuncs.com', 3717, username="zdiai",
                            password="Zd19092#", authSource="Repo_history", authMechanism="SCRAM-SHA-1")
USER_MONGO = conn["Repo_history"]


def col_names():
    result = USER_MONGO.list_collection_names(session=None)  # pymongo获取指定数据库的集合名称
    dbs = conn.list_database_names()  # pymongo获取mongodb实例下所有数据库名称
    for col in result:
        if col.find('_1hour') > 0:
            collect = USER_MONGO[col]
            ship_location = collect.find({"timestamp": {"$gt": 1621121092}}, {"Lat": 1, "Lon": 1, "_id": 0}).limit(1).sort("timestamp", -1)
            ship_location_obj = list(ship_location)
            if len(ship_location_obj) > 0 and len(ship_location_obj[0]) > 0:
                print(col, ship_location_obj[0]['Lon'], ship_location_obj[0]['Lat'])
            else:
                print(col)
    return result


def show_count():
    result = USER_MONGO.list_collection_names(session=None)  # pymongo获取指定数据库的集合名称
    dbs = conn.list_database_names()  # pymongo获取mongodb实例下所有数据库名称
    for col in result:
        if col.find('_1hour') > 0:
            continue
        elif col.find('_1day') > 0:
            continue
        elif col.find('_One') > 0:
            continue
        else:
            collect = USER_MONGO[col]
            count = collect.count()
            print(collect.name + "=" + str(count))

print(show_count())
